﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.MessagingManage
{
    public partial class EmailShow : Common.PageBase
    {
        private T_EMAIL_SEND_BLL emailSendBll = new T_EMAIL_SEND_BLL();
        private T_EMAIL_RECV_BLL emailRecvBll = new T_EMAIL_RECV_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pageDataInit();
            }
        }

        private void pageDataInit()
        {
            String EmailType = PageHelper.GetQueryString(this.Page, "EmailType");
            string AutoId = PageHelper.GetQueryString(this.Page, "AUTOID");
            switch (EmailType)
            {
                case "SEND":
                    {
                        T_EMAIL_SEND_Entity emailSendEntity = emailSendBll.GetEntity("AUTOID", AutoId);
                        txtFROM_NAME.Text = emailSendEntity.FROM_NAME;
                        txtTO_ADDR.Text = emailSendEntity.TO_ADDR;
                        txtCC_ADDR.Text = emailSendEntity.CC_ADDR;
                        txtBODYS.Text = emailSendEntity.BODYS;
                        txtSUBJECTS.Text = emailSendEntity.SUBJECTS;
                        HyperLink hl = new HyperLink();
                        if (!String.IsNullOrEmpty(emailSendEntity.ATTACH1))
                        {
                            hl.ID = "Attach1";
                            hl.Text = GetFileName(emailSendEntity.ATTACH1) + ";";
                            hl.NavigateUrl = emailSendEntity.ATTACH1;
                        }
                        else
                            hl.Visible = false;
                        plFILENAME.Controls.Add(hl);
                        hl = new HyperLink();
                        if (!String.IsNullOrEmpty(emailSendEntity.ATTACH2))
                        {
                            hl.ID = "Attach2";
                            hl.Text = GetFileName(emailSendEntity.ATTACH2) + ";"; ;
                            hl.NavigateUrl = emailSendEntity.ATTACH2;
                        }
                        else
                            hl.Visible = false;
                        plFILENAME.Controls.Add(hl);
                        hl = new HyperLink();
                        if (!String.IsNullOrEmpty(emailSendEntity.ATTACH3))
                        {
                            hl.ID = "Attach3";
                            hl.Text = GetFileName(emailSendEntity.ATTACH3) + ";"; ;
                            hl.NavigateUrl = emailSendEntity.ATTACH3;
                        }
                        else
                            hl.Visible = false;
                        plFILENAME.Controls.Add(hl);
                        hl = new HyperLink();
                        if (!String.IsNullOrEmpty(emailSendEntity.ATTACH4))
                        {
                            hl.ID = "Attach4";
                            hl.Text = GetFileName(emailSendEntity.ATTACH4) + ";"; ;
                            hl.NavigateUrl = emailSendEntity.ATTACH4;
                        }
                        else
                            hl.Visible = false;
                        plFILENAME.Controls.Add(hl);
                        hl = new HyperLink();
                        if (!String.IsNullOrEmpty(emailSendEntity.ATTACH5))
                        {
                            hl.ID = "Attach5";
                            hl.Text = GetFileName(emailSendEntity.ATTACH5);
                            hl.NavigateUrl = emailSendEntity.ATTACH5;
                        }
                        else
                            hl.Visible = false;
                        plFILENAME.Controls.Add(hl);
                    }
                    break; 
                case "RECEIVE":
                    {
                        T_EMAIL_RECV_Entity emailRecvEntity = emailRecvBll.GetEntity("AUTOID", AutoId);
                        txtFROM_NAME.Text = emailRecvEntity.FROM_NAME;
                        txtTO_ADDR.Text = emailRecvEntity.TO_ADDR;
                        txtCC_ADDR.Text = emailRecvEntity.CC_ADDR;
                        txtBODYS.Text = emailRecvEntity.BODYS;
                        txtSUBJECTS.Text = emailRecvEntity.SUBJECTS;
                        HyperLink hl = new HyperLink();
                        if (!String.IsNullOrEmpty(emailRecvEntity.ATTACH1))
                        {
                            hl.ID = "Attach1";
                            hl.Text = GetFileName(emailRecvEntity.ATTACH1) + ";"; ;
                            hl.NavigateUrl = emailRecvEntity.ATTACH1;
                        }
                        else
                            hl.Visible = false;
                        plFILENAME.Controls.Add(hl);
                        hl = new HyperLink();
                        if (!String.IsNullOrEmpty(emailRecvEntity.ATTACH2))
                        {
                            hl.ID = "Attach2";
                            hl.Text = GetFileName(emailRecvEntity.ATTACH2) + ";"; ;
                            hl.NavigateUrl = emailRecvEntity.ATTACH2;
                        }
                        else
                            hl.Visible = false;
                        plFILENAME.Controls.Add(hl);
                        hl = new HyperLink();
                        if (!String.IsNullOrEmpty(emailRecvEntity.ATTACH3))
                        {
                            hl.ID = "Attach3";
                            hl.Text = GetFileName(emailRecvEntity.ATTACH3) + ";"; ;
                            hl.NavigateUrl = emailRecvEntity.ATTACH3;
                        }
                        else
                            hl.Visible = false;
                        plFILENAME.Controls.Add(hl);
                        hl = new HyperLink();
                        if (!String.IsNullOrEmpty(emailRecvEntity.ATTACH4))
                        {
                            hl.ID = "Attach4";
                            hl.Text = GetFileName(emailRecvEntity.ATTACH4) + ";"; ;
                            hl.NavigateUrl = emailRecvEntity.ATTACH4;
                        }
                        else
                            hl.Visible = false;
                        plFILENAME.Controls.Add(hl);
                        hl = new HyperLink();
                        if (!String.IsNullOrEmpty(emailRecvEntity.ATTACH5))
                        {
                            hl.ID = "Attach5";
                            hl.Text = GetFileName(emailRecvEntity.ATTACH5);
                            hl.NavigateUrl = emailRecvEntity.ATTACH5;
                        }
                        else
                            hl.Visible = false;
                        plFILENAME.Controls.Add(hl);
                    }
                    break;
            }
        }

        private string GetFileName(string input)
        {
            return input.Substring(input.LastIndexOf("\\") + 1);
        }

    }
}
